//
//  TestViewController.m
//  MinBlog4Sina
//
//  Created by 水的右边 on 11-4-27.
//  Copyright 2011 http://www.cnblogs.com/hll2008/ All rights reserved.
//

#import "TestViewController.h"
#import "OAuthViewController.h"

#define kOAuthConsumerKey @"2433927322" //更换成你的
#define kOAuthConsumerSecret @"87f042c9e8183cbde0f005a00db1529f"//更换成你的

@implementation TestViewController
@synthesize label;
@synthesize authBtn;
@synthesize listBtn;
@synthesize addBtn;
@synthesize testBtn;

-(IBAction)doAuth:(id)sender
{
	UIViewController *controller=[blog getOAuthViewController:self];
	if (controller) {
		[self presentModalViewController: controller animated: YES];
	}
		
}


-(IBAction)getList:(id)sender
{
	if (blog) {
		NSLog(@"oauth is ok");
		[blog friends_timeline:3 page:1 delegate: self  onSuccess:@selector(getListSuccess: responseText:) onFail:@selector(getListFail: error:)];
	}else {
		NSLog(@"oauth is nil");
	}

}

-(void)getListSuccess:(NSNumber *)statusCode responseText:(NSString *)data
{
	NSLog(@"code:%@",statusCode);
	
	NSObject *obj = [data JSONValue];
	NSArray *ary = (NSArray*)obj; 
	for (int i=[ary count]-1; i>=0; --i) {
		Status* sts=[[Status alloc]initWithJsonDictionary:[ary objectAtIndex:i]];
		NSLog(@"user:%@",sts.retweeted_status);
	}
	
	//NSLog(@"bb:%@",obj);
	
}

-(void)getListFail:(NSNumber *)statusCode error:(NSString *)data
{
	NSLog(@"Error is:%@",data);
}

-(IBAction)addWeiBo:(id)sender
{
	if (blog) {
		NSLog(@"oauth is ok");
		//[blog update:@"这次肯定没有问题了把?" delegate:self onSuccess:@selector(addWeiBoSuccess: responseText:) onFail:@selector(addWeiBoFail: error:)];
		
		UIImage *image=[UIImage imageNamed:@"aaa.jpg"];
		NSData *imageData=UIImageJPEGRepresentation(image, 0.8);
		[blog upload:@"上传图片测试！！！" image:imageData filename:@"image.jpg" contentType:@"image/jpeg" delegate:self onSuccess:@selector(addWeiBoSuccess: responseText:) onFail:@selector(addWeiBoFail: error:)];
	}
}

-(void)addWeiBoSuccess:(NSNumber *)statusCode responseText:(NSString *)data
{
	NSLog(@"code:%@",statusCode);
	

	//NSLog(@"bb:%@",obj);
	
}

-(void)addWeiBoFail:(NSNumber *)statusCode error:(NSString *)data
{
	NSLog(@"Error is:%@",data);
}

-(IBAction)testEvt:(id)sender
{
	if (blog) {
		[blog show:blog.oauth.userID user_id:@"" screen_name:@"" delegate: self  onSuccess:@selector(getUserSuccess: responseText:) onFail:@selector(getUserFail: error:)];
	}
	NSLog(@"user_id:%@",blog.oauth.userID);
}

-(void)getUserSuccess:(NSNumber *)statusCode responseText:(NSString *)data
{
	NSLog(@"code:%@",statusCode);

	NSObject *obj = [data JSONValue];
	User *user=[[User alloc] initWithDictionary:(NSDictionary *)obj];
	NSLog(@"html:%@",user.screen_name);
	
}

-(void)getUserFail:(NSNumber *)statusCode error:(NSString *)data
{
	NSLog(@"Error is:%@",data);
}


-(IBAction)commentsEvt:(id)sender
{
	if (blog) {
		NSLog(@"oauth is ok");
		[blog comments_timeline:0 max_id:0 count:0 page:0 delegate: self  onSuccess:@selector(getCommentsSuccess: responseText:) onFail:@selector(getCommentsFail: error:)];
	}else {
		NSLog(@"oauth is nil");
	}
}

-(void)getCommentsSuccess:(NSNumber *)statusCode responseText:(NSString *)data
{
	//NSLog(@"code00:%@",data);
	
	NSObject *obj1 = [data JSONValue];
	NSArray *ary1 = (NSArray*)obj1; 
	for (int i=0; i<[ary1 count]; i++) {
		comment* ct=[[comment alloc]initWithJson:[ary1 objectAtIndex:i]];
		NSLog(@"user:%@",ct.user.name);
		NSLog(@"text:%@",ct.status.text);
	}
	
	//NSLog(@"bb:%@",obj);
	
}

-(void)getCommentsFail:(NSNumber *)statusCode error:(NSString *)data
{
	NSLog(@"Error is:%@",data);
}


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	blog=[[BlogClient alloc] initWithConsumerKey:kOAuthConsumerKey consumerSecret:kOAuthConsumerSecret];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[label release];
	[authBtn release];
	[listBtn release];
	[addBtn release];
	[blog release];
    [super dealloc];
}


@end
